<script setup>
  import { ref } from 'vue'
  import { useRoute } from 'vue-router'
  import { getActivity, getHomestay, getLibrary, getRoom } from '@/api/phone.js'
  import QRCode from 'qrcode'
  const route = useRoute()
  const detail = ref({})
  const type = ref('')
  const code = ref(false)
  const codeContent = ref('')
  const text = ref('')
  type.value = route.query.type || 'normal'
  function handleData(res) {
    detail.value = res.data
    if (detail.value.imgArray != null && detail.value.imgArray != '') {
      detail.value.imgArray = detail.value.imgArray.split(',')
    }
  }

  if (String(route.fullPath).includes('activity')) {
    getActivity(route.params.id).then(res => {
      handleData(res)
    })
    text.value = 'activity'
  }
  if (String(route.fullPath).includes('room')) {
    getRoom(route.params.id).then(res => {
      handleData(res)
    })
    text.value = 'room'
  }
  if (String(route.fullPath).includes('homestay')) {
    getHomestay(route.params.id).then(res => {
      handleData(res)
    })
    text.value = 'homestay'
  }
  if (String(route.fullPath).includes('library')) {
    getLibrary(route.params.id).then(res => {
      handleData(res)
    })
    text.value = 'library'
  }
  function callPhone(phoneNum) {
    window.location.href = 'tel://' + phoneNum
  }
  function handleShare() {
    let content = `${import.meta.env.VITE_DOMAIN_NAME}/${text.value}/${
      route.params.id
    }?type=share`
    QRCode.toDataURL(content, (err, url) => {
      codeContent.value = url
      code.value = true
    })
  }

  function downloadCode(url, name) {
    const image = new Image()
    image.src = url
    // 创建canvas元素
    const canvas = document.createElement('canvas')
    const ctx = canvas.getContext('2d')
    image.onload = () => {
      // 设置canvas大小
      canvas.width = image.width
      canvas.height = image.height + 30
      // 绘制背景色
      ctx.fillStyle = 'white' // 背景颜色
      ctx.fillRect(0, 0, canvas.width, canvas.height)
      // 绘制图片
      ctx.drawImage(image, 0, 0, image.width, image.height)
      // 设置文字样式
      ctx.font = '16px Arial'
      ctx.fillStyle = 'black' // 文字颜色
      ctx.textAlign = 'center' // 文字居中
      // 绘制文字
      ctx.fillText(name, canvas.width / 2, image.height + 10)
      // 将canvas转换为Base64编码
      const dataURL = canvas.toDataURL('image/png')
      // 创建一个链接元素
      const link = document.createElement('a')
      link.href = dataURL
      link.download = `${name}.png` // 设置下载的文件名
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    }
  }
</script>

<template>
  <head-title title="详情" :type="type"></head-title>
  <div class="detail">
    <el-carousel :interval="2000" arrow="never" indicator-position="none">
      <el-carousel-item v-for="item in detail.imgArray" :key="item">
        <img
          v-cover="item"
          style="width: 100%; height: 234px; object-fit: cover"
        />
      </el-carousel-item>
    </el-carousel>
    <div class="box">
      <div class="flex-between top">
        <div class="left">
          <div class="title">{{ detail.title }}</div>
          <div class="price" v-if="detail.price">
            {{ detail.price }}
          </div>
        </div>
        <div class="right" @click="handleShare">
          <img src="/src/assets/img/share.png" class="share" />
          分享
        </div>
      </div>
      <div class="flex-between one" v-if="detail.address || detail.phone">
        <div class="address-box">
          <p class="address">{{ detail.address }}</p>
          <p class="phone-box" v-if="detail.phone">
            <img src="/src/assets/img/phone.png" class="phone" />
            <span class="number">{{ detail.phone }}</span>
          </p>
        </div>
        <div class="flex-between">
          <div
            class="button"
            @click="callPhone(detail.phone)"
            v-if="detail.phone"
          >
            <img src="/src/assets/img/telephone.png" alt="" />
            <p>电话</p>
          </div>
        </div>
      </div>
      <div class="gray"></div>
      <div class="title detail">图文详情</div>
      <div class="content" v-html="detail.content"></div>
    </div>
  </div>
  <el-dialog title="分享" width="250px" align-center v-model="code">
    <div style="display: flex; flex-direction: column">
      <img :src="codeContent" alt="天台茶旅分享" style="" />
      <div style="text-align: center">
        {{ detail.title }}
      </div>
    </div>
    <div style="display: flex; justify-content: center; padding: 10px 0">
      <el-button
        type="primary"
        plain
        @click="downloadCode(codeContent, detail.title)"
        >下载
      </el-button>
    </div>
  </el-dialog>
</template>
<style>
  .el-carousel__container {
    height: 234px;
  }
</style>
<style lang="scss" scoped>
  .gray {
    height: 8px;
    background: #f5f5f5;
    margin: 0px -15px;
  }

  .detail {
    margin-top: 16px;
    margin-bottom: 12px;
  }

  .one {
    margin-top: 13px;
    padding-bottom: 20px;

    .button {
      img {
        width: 26px;
        height: 26px;
      }

      p {
        width: 24px;
        height: 17px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 12px;
        color: #222222;
        line-height: 17px;
        text-align: left;
        font-style: normal;
      }

      &:nth-child(2) {
        margin-left: 43px;
      }

      &:nth-child(3) {
        margin-left: 24px;
      }
    }
  }

  .address-box {
    .address {
      font-family: PingFangSC, PingFang SC;
      font-weight: bold;
      font-size: 14px;
      color: #333333;
    }

    .phone-box {
      display: flex;
      align-items: center;
    }

    .phone {
      width: 16px;
      height: 16px;
    }

    .number {
      height: 20px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 14px;
      color: #777777;
      line-height: 20px;
    }
  }

  .top {
    padding-bottom: 10px;
    border-bottom: 1px solid #f5f5f5;
  }

  .right {
    width: 82px;
    height: 36px;
    background: linear-gradient(131deg, #8dec82 0%, #4ad26f 100%);
    border-radius: 100px 0px 0px 100px;
    margin-right: -15px;
    line-height: 36px;
    text-align: center;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    font-size: 14px;
    color: #ffffff;

    .share {
      width: 18px;
      height: 18px;
      vertical-align: middle;
    }
  }

  .box {
    background: #fff;
    border-radius: 12px 12px 0px 0px;
    padding: 16px;
    margin-top: -15px;
    z-index: 1;
    position: relative;

    .title {
      font-family: PingFangSC, PingFang SC;
      font-weight: 600;
      font-size: 20px;
      color: #333333;
      line-height: 26px;
    }

    .price {
      height: 33px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 600;
      font-size: 24px;
      color: #107e2a;
      line-height: 33px;

      .unit {
        height: 17px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 12px;
        color: #107e2a;
        line-height: 17px;
      }
    }
  }
</style>
